﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Text;
using System.Web.UI;
using System.IO;

namespace MongoDbWebClient.Extensions
{
    public static class ManagerExtensions
    {
        /// <summary>
        /// Render top menu dependent to User connection state;
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static MvcHtmlString RenderTopMenu(this HtmlHelper html)
        {
            UrlHelper url = new UrlHelper(html.ViewContext.RequestContext);
            StringBuilder sb = new StringBuilder();
            using (HtmlTextWriter writer = GetHtmlWriter(sb))
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);
                writer.RenderBeginTag("li");
                CreateAjaxLink(html,writer, "$.connectToServer()", "Connect");
                writer.RenderEndTag();
                writer.RenderEndTag();
            }

            return new MvcHtmlString(sb.ToString());
        }

        private static HtmlTextWriter GetHtmlWriter(StringBuilder sb)
        {
            return new HtmlTextWriter(new StringWriter(sb));
        }



        public static MvcHtmlString CreateAjaxLink(this HtmlHelper html,string javaAction,string text)
        {
            StringBuilder sb = new StringBuilder();
            using (HtmlTextWriter writer = GetHtmlWriter(sb))
            {
                CreateAjaxLink(html, writer, javaAction, text);
            }
            return new MvcHtmlString(sb.ToString());
        }


        public static void CreateAjaxLink(this HtmlHelper html, HtmlTextWriter writer, string javaAction, string text)
        {
             writer.AddAttribute(HtmlTextWriterAttribute.Href, string.Format("javascript:{0}", javaAction));
             writer.RenderBeginTag(HtmlTextWriterTag.A);
             writer.Write(text);
             writer.RenderEndTag();
        }
    }
}